﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Threading;

namespace FlightControl
{
    public partial class MainPage : UserControl
    {

        DispatcherTimer gameTimer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(33) };
        public MainPage()
        {
            // Required to initialize variables
            InitializeComponent();

            gameTimer.Tick += new EventHandler(gameTimer_Tick);
            gameTimer.Start();
            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
        }

        void gameTimer_Tick(object sender, EventArgs e)
        {
            RenderAll();
        }

        void RenderAll()
        {
            var planes = planeCanvas.Children.Where(c => c is Plane).Select(p => p as Plane);
            foreach (var plane in planes)
            {
                foreach (var possibleHit in planes)
                {
                    if (possibleHit != plane)
                    {
                        double distance = Math.Sqrt(Math.Pow((possibleHit.Position.X - plane.Position.X), 2) + Math.Pow((possibleHit.Position.Y - plane.Position.Y), 2));
                        debug.Text = distance.ToString();
                        if (distance < 120)
                            plane.Danger = true;
                        else
                            plane.Danger = false;

                    }
                }

                
                plane.Update();
            }
        }
        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            debug.Text = debug.Text;

        }

    }
}